import json

from dtcloud.fields import Field
from dtcloud.tools import json_default
from dtcloud.models import Model
from .BaseConvert import Convert


class JsonConvert(Convert):
    field_type = 'json'

    @classmethod
    def to_read(cls, rec: Model, field: Field, **kwargs):
        return json.loads(json.dumps(rec[field.name], default=json_default)) if rec[field.name] else None

    @classmethod
    def to_field(cls, value, metadata=None, update=False):
        return value
